package com.midea.ai.air.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.midea.ai.air.R;

import java.util.ArrayList;

/**
 * Created by hww on 2016/4/11.
 * 空气报告 曲线图
 */
public class AirReportChartView extends View {

    private static final int CIRCLE_SIZE = 10;

    private static enum LineStyle {
        Line, Circle
    }

    private LineStyle mStyle = LineStyle.Line;

    private Context mContext;
    private Resources Res;
    private DisplayMetrics dm;
    private int mCanvasWidth, mCanvasHeight; // view的宽高

    private boolean isMeasure = true;

    private static final int spacingHeight = 4;// 高度的间隔数

    private static final int maxValueY = 8;// Y轴最大值
    private static final int averageValueY = 2;// Y轴间距值

    private static final int averageValyeX = 24;// X轴间距值
    private Point[] mPoints_one;// 曲线上的总点数
    private Point[] mPoints_two;// 曲线上的总点数

    private Point[] mPoints_three;// 曲线上的总点数
    private ArrayList<Double> yRawData_one;// 纵坐标值
    private ArrayList<Double> yRawData_two;
    private ArrayList<Double> yRawData_three;

    private ArrayList<String> xRawData = new ArrayList<String>();// 横坐标值

    private ArrayList<Integer> xList = new ArrayList<Integer>();// 记录每个x的值

    private Path path_one;
    private Path path_two;
    private Path path_three;

    private Paint mPaint;// 坐标曲线的属性
    private Paint mPaint_one;// 曲线的属性
    private Paint mPaintBg_one;// 报表背景画笔
    private Paint mPaintBg_two;
    private Paint mPaintBg_three;

    private int mColor_one;
    private int mColor_two;
    private int mColor_three;

    private Paint mPaintChartLine;// 表格的网格

    private Paint mPaint_chart_bg;// 报表背景画笔

    public AirReportChartView(Context context) {
        this(context, null, 0);
    }

    public AirReportChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AirReportChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        this.Res = mContext.getResources();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_one = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_chart_bg = new Paint();
        mPaintBg_one = new Paint();
        mPaintBg_two = new Paint();
        mPaintBg_three = new Paint();
        path_one = new Path();
        path_two = new Path();
        path_three = new Path();
        mPoints_one = new Point[averageValyeX];
        dm = new DisplayMetrics();
        WindowManager vm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        vm.getDefaultDisplay().getMetrics(dm);

        if (xRawData.size() == 0) {
            for (int i = 0; i < averageValyeX; i++) {
                xRawData.add((i + 1) + "");
            }
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (isMeasure) {
            this.mCanvasHeight = getHeight();
            this.mCanvasWidth = getWidth();
            isMeasure = false;

            for (int i = 0; i < averageValyeX; i++) {
                xList.add(mCanvasWidth / averageValyeX * i);
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d("hwwlog", "mPaint_one :" + mPaint_one);
        mPaint_chart_bg.setColor(Res.getColor(R.color.common_white));
        mPaint_chart_bg.setStyle(Paint.Style.FILL);
        canvas.drawRect(0, 0, mCanvasWidth, mCanvasHeight, mPaint_chart_bg);

        mPaintBg_one.setColor(Res.getColor(mColor_three));
        mPaintBg_one.setStyle(Paint.Style.FILL);
        mPaintBg_one.setAlpha(50);
        mPaintBg_two.setColor(Res.getColor(mColor_two));
        mPaintBg_two.setStyle(Paint.Style.FILL);
        mPaintBg_two.setAlpha(50);
        mPaintBg_three.setColor(Res.getColor(mColor_one));
        mPaintBg_three.setStyle(Paint.Style.FILL);
        mPaintBg_three.setAlpha(50);
        mPaint_one.setStrokeWidth(0.5f);
        mPaint_one.setAntiAlias(true);
        mPaint_one.setStyle(Paint.Style.STROKE);

        // 第一块的内容(昨天的电量使用情况)
        // 点的操作设置
        mPoints_one = getPoints(yRawData_one);
        mPaint_one.setColor(Res.getColor(mColor_one));
        mPaint_one.setAlpha(30);
        if (mStyle == LineStyle.Circle) {
            drawScrollLine(canvas, mPoints_one, mPaint_one);
        } else {
            drawLine(canvas, mPaintBg_one, mPoints_one, path_one, mPaint_one);
        }

        // 第二块内容（用作对比的）
        mPoints_two = getPoints(yRawData_two);
        mPaint_one.setColor(Res.getColor(mColor_two));
        mPaint_one.setAlpha(50);
        if (mStyle == LineStyle.Circle) {
            drawScrollLine(canvas, mPoints_two, mPaint_one);
        } else {
            drawLine(canvas, mPaintBg_two, mPoints_two, path_two, mPaint_one);
        }

        // 第三块内容（当前电量使用情况）
        mPoints_three = getPoints(yRawData_three);
        mPaint_one.setColor(Res.getColor(mColor_three));
        mPaint_one.setAlpha(50);
        if (mStyle == LineStyle.Circle) {
            drawScrollLine(canvas, mPoints_three, mPaint_one);
        } else {
            drawLine(canvas, mPaintBg_three, mPoints_three, path_three, mPaint_one);
        }

        mPaint.setColor(Res.getColor(R.color.electric_histogram));
        mPaint.setStrokeWidth(0.5f);
        mPaint.setStyle(Paint.Style.STROKE);
        drawAllLine(canvas, mPaint);

        // mPaint_one.setStyle(Paint.Style.FILL);
        // for (int i = 0; i < mPoints.length; i++) {
        // //绘制小圆点
        // canvas.drawCircle(mPoints[i].x, mPoints[i].y, CIRCLE_SIZE / 2,
        // mPaint_one);
        // }
    }

    /**
     * 画所有表格，包括X Y轴
     */
    private void drawAllLine(Canvas canvas, Paint paint) {

        for (int i = 0; i < spacingHeight + 1; i++) {
            // canvas.drawLine(bWidth,bHeight - (bHeight / spacingHeight) * i,
            // (mCanvasWidth - bWidth),
            // bHeight - (bHeight / spacingHeight) * i, mPaint_one);//X坐标
            // canvas.drawText(String.valueOf(averageValueY * i), mCanvasWidth -
            // 18,
            // mCanvasHeight - (mCanvasHeight / spacingHeight) * i + 10, paint);
            drawText(String.valueOf(averageValueY * i), mCanvasWidth - 18,
                    mCanvasHeight - (mCanvasHeight / spacingHeight) * i + 15, canvas);
        }

        int show = 1;
        for (int i = 0; i < averageValyeX; i++) {
            canvas.drawLine(mCanvasWidth / averageValyeX * i, 0, mCanvasWidth / averageValyeX * i, mCanvasHeight,
                    paint);// Y坐标

            int x = ((mCanvasWidth / averageValyeX * i) + (mCanvasWidth / averageValyeX * (i + 1))) / 2;
            if (i == show) {
                drawText(xRawData.get(i), x, mCanvasHeight - 12, canvas);
                show += 4;
            }
        }
    }

    private void drawScrollLine(Canvas canvas, Point[] mPoints, Paint paint) {
        Point startp = new Point();
        Point endp = new Point();
        for (int i = 0; i < mPoints.length - 1; i++) {
            startp = mPoints[i];
            endp = mPoints[i + 1];
            int wt = (startp.x + endp.x) / 2;
            Point p3 = new Point();
            Point p4 = new Point();
            p3.y = startp.y;
            p3.x = wt;
            p4.y = endp.y;
            p4.x = wt;

            Path path = new Path();
            path.moveTo(startp.x, startp.y);
            path.cubicTo(p3.x, p3.y, p4.x, p4.y, endp.x, endp.y);
            canvas.drawPath(path, paint);
        }
    }

    /**
     * @param canvas
     * @param mPaintBG 背景颜色
     * @param mPoints  定点
     * @param path_all 设置背景颜色的路径
     * @param paint    画线的画笔
     */
    private void drawLine(Canvas canvas, Paint mPaintBG, Point[] mPoints, Path path_all, Paint paint) {
        Point startp = new Point();
        Point endp = new Point();
        float endx = 0;
        for (int i = 0; i < mPoints.length - 1; i++) {
            startp = mPoints[i];
            endp = mPoints[i + 1];
            canvas.drawLine(startp.x, startp.y, endp.x, endp.y, paint);

            path_all.lineTo(startp.x, startp.y);
            path_all.lineTo(endp.x, endp.y);
            endx = endp.x;
        }

        path_all.lineTo(endx, mCanvasHeight);
        path_all.lineTo(0, mCanvasHeight);
        path_all.lineTo(mPoints[0].x, mPoints[0].y);
        canvas.drawPath(path_all, mPaintBG);
    }

    private void drawText(String text, int x, int y, Canvas canvas) {
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setTextSize(18);
        p.setColor(Res.getColor(R.color.black_standard));
        p.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text, x, y, p);
    }

    private Point[] getPoints(ArrayList<Double> yRawData) {
        Point[] points = new Point[yRawData.size()];
        for (int i = 0; i < yRawData.size(); i++) {
            int ph = mCanvasHeight - (int) (mCanvasHeight * (yRawData.get(i) / maxValueY));

            points[i] = new Point(xList.get(i), ph);
        }
        return points;
    }

    public void setDatas(ArrayList<Double> yList_one, ArrayList<Double> yList_two, ArrayList<Double> yList_three,
                         int mColor_one, int mColor_two, int mColor_three) {
        this.yRawData_one = yList_one;
        this.yRawData_two = yList_two;
        this.yRawData_three = yList_three;

        this.mColor_one = mColor_one;
        this.mColor_two = mColor_two;
        this.mColor_three = mColor_three;
    }
}
